import { SelectProps } from 'antd';
import { ProFormSelect } from '@ant-design/pro-form';

//获取角色

type formProps = {
  id?: number;
  name?: string;
  label?: string;
  busType?: number; //{1:用户}
  fieldType?: number; //{1:文本框，2：单选，3：多选，4：日期}
  isDefault?: number;
  isRequire?: number;
  isEnable?: number;
  multiple?: number;
  fieldOption: string;
  describe?: string;
  createDate?: string;
  order?: number;
  max?: number;
  min?: number;
  regEx?: string;
  precision?: number; //保留几位小数
  disabled?: any;
  fieldProps?: SelectProps<any> & any;
  rule?: any;
  placeholder: string;
};

export default (props: formProps) => {
  const {
    label,
    isEnable,
    fieldType,
    isDefault,
    name,
    id,
    isRequire,
    regEx,
    min,
    max,
    precision,
    fieldOption,
    disabled,
    fieldProps = {},
    rule = [],
    placeholder,
    multiple,
    ...restProps
  } = props;
  let _name = isDefault ? name : `custom_${id}_${fieldType}`;

  //验证修改
  !!isRequire &&
    rule.push({
      required: true,
      message: `请输入${label}`,
    });
  !isEnable && (rule.length = 0);

  //组件配置修改
  // !isDefault && (fieldProps.labelInValue = true); //返回选项所有值
  fieldProps.disabled = disabled; //表单禁用选项

  return (
    <ProFormSelect
      {...restProps}
      name={_name}
      preserve
      rules={rule}
      label={label}
      placeholder={placeholder}
      fieldProps={{ mode: !!multiple ? 'multiple' : '', ...fieldProps }}
      options={
        !!fieldOption
          ? JSON.parse(fieldOption).map((item: any) => {
              return (item.value = item.label);
            })
          : []
      }
    />
  );
};
